import { configureStore } from '@reduxjs/toolkit'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' // 使用 localStorage 作为默认存储
import messageSlice from './messageSlice'
import layoutSlice from './layoutSlice'

const store = configureStore({
  reducer: {
    message: persistReducer(
      {
        key: 'messageSlice', // 存储的 key
        storage // 存储方式
      },
      messageSlice
    ), // 使用持久化的 reducer
    layout: persistReducer(
      {
        key: 'layoutSlice', // 存储的 key
        storage // 存储方式
      },
      layoutSlice
    ) // 使用持久化的 reducer
  },
  middleware: getDefaultMiddleware =>
    getDefaultMiddleware({
      serializableCheck: {
        // 忽略 redux-persist 的特定 action
        ignoredActions: ['persist/PERSIST', 'persist/REHYDRATE']
      }
    })
})

export const persistor = persistStore(store) // 创建持久化存储器

type GetStateFunType = typeof store.getState

export type IRoorState = ReturnType<GetStateFunType> // 定义TS类型，使用时出现提示

export default store
